<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "org.json.simple.*, com.classes.Casemast, com.classes.CaseOutlet, java.util.ArrayList, java.util.TreeMap" %>
<%@ page import = "com.mbbmap.util.Constants" %>
<%@ page import = "com.mbbmap.security.dao.*" %>
<%@ include file="/jsp/map/include/user-dao.jsp" %>	
<%
long merchant_id = 0;
long case_id = 0;
long outlet_id = 0;
String submission_date = "";
String start_date = "";
String dcc_merchant_no = "00002702";
String mbb_merchant_no = "00002700";
String mcc_code = "";
String merchant_category = "";
String merchant_name = "";
String trading_name = "";
String address1 = "";
String address2 = "";
String postal_code = "";
String city = "";
String region_code = "";
String country = "MALAYSIA";
String contact_title = "";
String contact_name = "";
String contact_surname = "";
String contact_tel = "";
String contact_escalate_tel = "";
String contact_mobile = "";
String contact_fax = "";
String contact_email = "";
String contact_generic_email = "";
String dcc_rebate = "";
String hod_approval = "Approve";

JSONObject vendor_arr = new JSONObject();
ArrayList<TreeMap<String, String>> tid_facilities = new ArrayList<TreeMap<String, String>>();

JSONObject appendix = new JSONObject();
JSONObject appdx_dcc= new JSONObject();
JSONObject appdx_data= new JSONObject();

if(request.getAttribute("m_case") != null){
	Casemast m_case = (Casemast) request.getAttribute("m_case");
	
	merchant_id = m_case.getMerchant().getId();
	case_id = m_case.getId();
	submission_date = m_case.getSubmitDate() > 0 ? Casemast.formatCreatedDate("dd/MM/yyyy HH:mm", m_case.getSubmitDate()) : "";
	mcc_code = m_case.getMerchant().getMCCCode();
	merchant_name = m_case.getMerchant().getRegisteredName();
}


if(request.getAttribute("c_outlet") != null) {
	CaseOutlet c_outlet = (CaseOutlet) request.getAttribute("c_outlet");
	
	outlet_id = c_outlet.getOutlet().getId();
	trading_name = c_outlet.getOutlet().getName();
	
	JSONObject addr_obj = new JSONObject();
	if(c_outlet.getOutlet().getAddress().length() > 0){
		addr_obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getAddress());
	}
	
	address1 = (addr_obj.get("line_1") == null) ? "" : addr_obj.get("line_1").toString().trim();
	address2 = (addr_obj.get("line_2") == null) ? "" : addr_obj.get("line_2").toString().trim();
	postal_code = (addr_obj.get("postal_code") == null) ? "" : addr_obj.get("postal_code").toString().trim();
	city = (addr_obj.get("city") == null) ? "" : addr_obj.get("city").toString().trim();
	region_code = (addr_obj.get("region_code") == null) ? "" : addr_obj.get("region_code").toString().trim();
	
	if(region_code.equals("002")) {
		region_code = "Sabah";
	} else if(region_code.equals("003")) {
		region_code = "Sarawak";
	} else if(region_code.equals("004")) {
		region_code = "Perlis";
	} else if(region_code.equals("005")) {
		region_code = "Kedah";
	} else if(region_code.equals("006")) {
		region_code = "Pulau Pinang";
	} else if(region_code.equals("007")) {
		region_code = "Perak";
	} else if(region_code.equals("008")) {
		region_code = "Kelantan";
	} else if(region_code.equals("009")) {
		region_code = "Terengganu";
	} else if(region_code.equals("010")) {
		region_code = "Pahang";
	} else if(region_code.equals("011")) {
		region_code = "Selangor";
	} else if(region_code.equals("012")) {
		region_code = "Negeri Sembilan";
	} else if(region_code.equals("013")) {
		region_code = "Melaka";
	} else if(region_code.equals("014")) {
		region_code = "Johor";
	} else if(region_code.equals("015")) {
		region_code = "Wilayah Persekutuan";
	}
	
	
	JSONObject mdr_obj = new JSONObject();
	if(c_outlet.getOutlet().getMDRRate().length() > 0){
		mdr_obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getMDRRate());
	}
	
	dcc_rebate = mdr_obj.get("166") == null ? "" : mdr_obj.get("166").toString();
	
	if(c_outlet.getPosForm().length() > 0){
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getPosForm());
		
		if(obj.get("vendor_arr") != null){
			vendor_arr = (JSONObject) obj.get("vendor_arr");
		}
		
		if(obj.get("appendix") != null) {
			appendix = (JSONObject) obj.get("appendix");
		}
		
		if(appendix.get("DCC") != null){
			appdx_dcc = (JSONObject) appendix.get("DCC");
		}
		
		if(appdx_dcc.get("data") != null){
			appdx_data = (JSONObject) appdx_dcc.get("data");
		}
		
		
		start_date = appdx_data.get("start_date") == null ? "" : appdx_data.get("start_date").toString();
		dcc_merchant_no = appdx_data.get("dcc_merchant_no") == null ? dcc_merchant_no : appdx_data.get("dcc_merchant_no").toString();
		mbb_merchant_no = appdx_data.get("mbb_merchant_no") == null ? mbb_merchant_no : appdx_data.get("mbb_merchant_no").toString();
		
		contact_title = appdx_data.get("contact_title") == null ? "" : appdx_data.get("contact_title").toString();
		contact_name = appdx_data.get("contact_name") == null ? "" : appdx_data.get("contact_name").toString();
		contact_surname = appdx_data.get("contact_surname") == null ? "" : appdx_data.get("contact_surname").toString();
		contact_tel = appdx_data.get("contact_tel") == null ? "" : appdx_data.get("contact_tel").toString();
		contact_escalate_tel = appdx_data.get("contact_escalate_tel") == null ? "" : appdx_data.get("contact_escalate_tel").toString();
		contact_mobile = appdx_data.get("contact_mobile") == null ? "" : appdx_data.get("contact_mobile").toString();
		contact_fax = appdx_data.get("contact_fax") == null ? "" : appdx_data.get("contact_fax").toString();
		contact_email = appdx_data.get("contact_email") == null ? "" : appdx_data.get("contact_email").toString();
		contact_generic_email = appdx_data.get("contact_generic_email") == null ? "" : appdx_data.get("contact_generic_email").toString();
		
	}
}


for(Object q : vendor_arr.keySet()){
	JSONObject vendor_details = (JSONObject) vendor_arr.get(q);
	JSONObject facility_tids = (vendor_details.get("facility_tids") == null ? null : (JSONObject) vendor_details.get("facility_tids"));
	
	String[] arr_type = new String[] {"Wired", "Wireless", "MPOS"};
	
	for(Object s : facility_tids.keySet()){
		JSONObject tid_type_obj = (JSONObject) facility_tids.get(s);
		
		for(String t  : arr_type) {
			if(tid_type_obj.get(t) != null) {
				JSONArray tid_obj = (JSONArray) tid_type_obj.get(t);
				
				for(int i=0; i<tid_obj.size(); i++){
					JSONObject tid_item = (JSONObject) tid_obj.get(i);
					
					TreeMap<String, String> item = new TreeMap<String, String>();
					item.put("vendor", q.toString());
					item.put("facility", s.toString());
					item.put("mid_no", tid_item.get("mid_no").toString());
					item.put("tid_no", tid_item.get("tid_no").toString());
					
					tid_facilities.add(item);
				}
			}
		}
	}
}

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/jsp/map/include/html-meta.jsp" />
<title>MAYBANK</title>
</head>
<body>

<!-- Header -->
<%@ include file="/jsp/common/_Header_Email.jsp" %>	
<!-- End Header -->

<div class="row-fluid page">
	<div class="span12">
		
		<form name="form">
		<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
		<div class="form-horizontal">
			
			<div class="control-group">
				<label class="control-label">Merchant Application Date</label>
				<div class="controls">
					<input type="text" id="submission_date" name="submission_date" readonly="readonly" value="<%= submission_date %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Merchant Target Start Date</label>
				<div class="controls">
					<input type="text" id="start_date" name="start_date" readonly="readonly" value="<%= start_date %>" />
				</div>
			</div>
			
			
			<div class="row-fluid">
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">DCC Merchant Number</label>
						<div class="controls">
							<input type="text" id="dcc_merchant_no" name="dcc_merchant_no" readonly="readonly" value="<%= dcc_merchant_no %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">MBB Merchant Number</label>
						<div class="controls">
							<input type="text" id="mbb_merchant_no" name="mbb_merchant_no" readonly="readonly" value="<%= mbb_merchant_no %>" />
						</div>
					</div>
					
				</div>
				
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">Merchant Category (MCC) Code</label>
						<div class="controls">
							<input type="text" id="mcc_code" name="mcc_code" readonly="readonly" value="<%= mcc_code %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Merchant Category (Travelex Use Only)</label>
						<div class="controls">
							<input type="text" id="merchant_category" name="merchant_category" readonly="readonly" value="<%= merchant_category %>" />
						</div>
					</div>
					
				</div>
			</div>
			
			
			<div class="control-group">
				<label class="control-label">Merchant Name (Legal Name)</label>
				<div class="controls">
					<input type="text" id="merchant_name" name="merchant_name" readonly="readonly" value="<%= merchant_name %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Trading Name</label>
				<div class="controls">
					<input type="text" id="trading_name" name="trading_name" readonly="readonly" value="<%= trading_name %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Address</label>
				<div class="controls">
					<input type="text" id="address1" name="address1" readonly="readonly" value="<%= address1 %>" />
				</div>
				<div class="controls">
					<input type="text" id="address2" name="address2" readonly="readonly" value="<%= address2 %>" />
				</div>
			</div>
			
			
			<div class="row-fluid">
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">City / Suburb</label>
						<div class="controls">
							<input type="text" id="city" name="city" readonly="readonly" value="<%= city %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Country</label>
						<div class="controls">
							<input type="text" id="country" name="country" readonly="readonly" value="<%= country %>" />
						</div>
					</div>
					
				</div>
				
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">State</label>
						<div class="controls">
							<input type="text" id="region_code" name="region_code" readonly="readonly" value="<%= region_code %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Postcode</label>
						<div class="controls">
							<input type="text" id="postal_code" name="postal_code" readonly="readonly" value="<%= postal_code %>" />
						</div>
					</div>
					
				</div>
			</div>
			
			
			<div class="control-group">
				<label class="control-label">Primary Business Contact</label>
				<div class="controls">
					<input type="text" id="contact_title" name="contact_title" placeholder="Title" readonly="readonly" value="<%= contact_title %>" style="width:70px" />
					<input type="text" id="contact_name" name="contact_name" placeholder="Given Name" readonly="readonly" value="<%= contact_name %>" />
					<input type="text" id="contact_surname" name="contact_surname" placeholder="Surname" readonly="readonly" value="<%= contact_surname %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Telephone Number</label>
				<div class="controls">
					<input type="text" id="contact_tel" name="contact_tel" readonly="readonly" value="<%= contact_tel %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Escalation Telephone Number</label>
				<div class="controls">
					<input type="text" id="contact_escalate_tel" name="contact_escalate_tel" readonly="readonly" value="<%= contact_escalate_tel %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Mobile</label>
				<div class="controls">
					<input type="text" id="contact_mobile" name="contact_mobile" readonly="readonly" value="<%= contact_mobile %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Facsimile Number</label>
				<div class="controls">
					<input type="text" id="contact_fax" name="contact_fax" readonly="readonly" value="<%= contact_fax %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Email</label>
				<div class="controls">
					<input type="text" id="contact_email" name="contact_email" readonly="readonly" value="<%= contact_email %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Generic Email for Daily Opt-in Report</label>
				<div class="controls">
					<input type="text" id="contact_generic_email" name="contact_generic_email" readonly="readonly" value="<%= contact_generic_email %>" />
				</div>
			</div>
			
			
			<div class="row-fluid">
				<div class="span6">
				
					<div class="control-group">
						<label class="control-label">Merchant DCC Rebate (%)</label>
						<div class="controls">
							<input type="text" id="dcc_rebate" name="dcc_rebate" readonly="readonly" value="<%= dcc_rebate %>" />
						</div>
					</div>
					
				</div>
				
				<div class="span6">
				
					<div class="control-group">
						<label class="control-label">HOD Approval</label>
						<div class="controls">
							<input type="text" id="hod_approval" name="hod_approval" readonly="readonly" value="<%= hod_approval %>" />
						</div>
					</div>
					
				</div>
			</div>
			
			
			<table class="tbl vendor_pos_tbl_1" style="width:75%; margin-bottom:20px"><thead>
				<tr>
					<th style="width:33.33%">Terminal ID</th>
					<th style="width:33.33%">PID (Travelex Use Only)</th>
					<th style="width:33.33%">Terminal Details (Vendor/Model)</th>
				</tr>
			</thead><tbody>
			<%
				for(TreeMap<String, String> item : tid_facilities){
					String html = "<tr>"
						+ "	<td class=\"tid_no\"><input type=\"text\" readonly=\"readonly\" value=\"" + item.get("tid_no") + "\" /></td>"
						+ "	<td class=\"pid\"><input type=\"text\" readonly=\"readonly\" value=\"\" ></td>"
						+ "	<td class=\"vendor\"><input type=\"text\" readonly=\"readonly\" value=\"" + item.get("vendor") + "\" ></td>"
						+ "</tr>";
					
					out.println(html);
				}
			%>
			</tbody></table>
			
		</div>
		</form>
		
	</div>
</div>

</body>
</html>